// ------------------------------------------------------------------------------
// name: searchEngine 搜索引擎
// author: cookie( h5devs.com/h5devs.net )
// created: 2018/12/1 22:30
// type:case|police|keyPerson|offenMan|leaveMan
// ------------------------------------------------------------------------------
// state
const State = {
   param:{}
  };
  
  // getters
  const Getters = {
    getKeyWord: state => state.keyword,

  };
  
  // mutations
  const Mutations = {
    keyWordEdit(state, val) {
      state.keyword = val;
    },
    pushItem(state, data) {
      let { item, type } = data;
      let _data = state[type];
      // 判断是否有重复字段key,如果有，更新数据，没有就添加
      let bool = false;
      let index = 0;
      for (let i = 0; i < _data.length; i++) {
        let _item = _data[i];
        if (_item.key === item.key) {
          bool = true;
          index = i;
          break;
        }
      }
      if (bool) {
        _data.splice(index, 1, item);
        return;
      }
      _data.push(item);
      console.warn(state[type]);
    },
    deleteItem(state, data) {
      let {index,type} = data;
      state[type].splice(index, 1);
    }
  };
  
  // actions
  const Actions = {
    // keyword edit
    setKeyWord({
      commit
    }, wordValue) {
      commit('keyWordEdit', wordValue);
    },
    //arry item push
    setItem({
      commit
    }, data) {
      console.warn(`${data.item}--${data.type}`);
      commit('pushItem', data);
    },
    // arry item del
    removeItem({
      commit
    }, data) {
      commit('deleteItem', data);
    }
  };
  
  export default {
    namespaced: true,
    strict: true,
    state: State,
    getters: Getters,
    actions: Actions,
    mutations: Mutations
  };
  